// pages/search/search.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        freight: 0,
        arr: [],

        payType: 'wechat',
    },
    confirm() {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLoad: function(options) {

        console.log(JSON.parse(options.arr).data);
        this.setData({
            freight: options.data,
            arr: JSON.parse(options.arr).data,
            addressId: options.addressId
        })
    },
    changePayType(e) {
        this.setData({
            payType: e.currentTarget.dataset.type
        })
    },
    createOrder: function() {
        var _this = this

        if (_this.data.payType == 'wechat') {
            wx.request({
                url: app.globalData.url+'/v1/freight/pay',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    id: _this.data.arr,
                    orderName: Number(_this.data.freight),
                    openid: app.globalData.openId
                },
                success: function(r) {
                    if (r.data.code == 0) {
                        // console.log(r);
                        // // _this.beforePay(r.data.data.package)
                        // _this.pay(r.data.data.package)
                        _this.pay(r.data.data.package)

                    } else {
                        wx.showToast({
                            title: r.data.data.message,
                            icon: 'none',
                            duration: 4000
                        })
                    }
                }
            })
        } else {

            wx.request({
                url: app.globalData.url+'/v1/payment/freight',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                    openid: app.globalData.openId,
                    id: _this.data.arr
                },
                success: function(r) {
                    r = r.data
                    if (r.code == 0) {
                        wx.showToast({
                            title: r.data
                        })
                        _this.send();
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 3
                            })
                        }, 1500)
                        wx.hideToast()
                    } else {
                        wx.hideToast()
                        wx.showToast({
                            title: r.data.message,
                            icon: 'none'
                        })
                    }
                }
            })
        }
    },
    beforePay: function(data) {
        var _this = this
        wx.request({
            url: app.globalData.url+'/v1/wechat/pay',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                uid: data.uid,
                id: data.id,
                orderName: 'WOWSURPRISE有惊喜'
            },
            success: function(r) {
                _this.pay(r.data.data.package, data.id)
            }
        })
    },
    pay: function(data) {
        var _this = this
        wx.requestPayment({
            timeStamp: data.timeStamp,
            nonceStr: data.nonceStr,
            package: data.package,
            signType: 'MD5',
            paySign: data.paySign,
            success(res) {
                console.log(res);
                _this.send();
            },
            fail(res) {
                wx.showToast({
                    icon: 'none',
                    title: '用户取消支付',
                })
            }
        })
    },
    send: function() {
        var _this = this
        wx.showToast({
            title: 'loading...',
            icon: 'loading',
            duration: 60000
        })
        wx.request({
            url: app.globalData.url+'/v1/nihao/deliver',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                id: _this.data.arr.toString(),
                address_id: _this.data.addressId,
                openid: app.globalData.openId,
            },
            success: function(r) {
                r = r.data
                if (r.code == 0) {
                    wx.showToast({
                        title: r.data
                    })
                    setTimeout(function() {
                        wx.navigateBack({
                            delta: 3
                        })
                    }, 1500)
                    wx.hideToast()
                } else {
                    wx.hideToast()
                    wx.showToast({
                        title: r.data.message,
                        icon: 'none'
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})